<template>
  <div>
    <div class="flex wrap">
      <div v-if="selectedList.length === 0" class="link-none">
        <div class="add-btn" @click="addEnter">添加快捷入口</div>
        <div>您可以通过设置添加服务列表的快捷入口</div>
      </div>
      <div v-else class="link-wrapper" v-for="item in selectedList" :key="item" @click="goLink(linkObj[item].link)">{{linkObj[item].name}}</div>
    </div>
    <div class="line" v-if="historyList.length > 0"></div>
    <div v-if="historyList.length > 0">
      <div class="title">最近访问</div>
      <div class="flex">
        <div class="link-wrapper history-link" v-for="(item, index) in historyList" :key="index" @click="goLink(item.link)">{{item.name}}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { menuListData } from '../../../../assets/js/menu-list'
import _ from 'lodash'

export default {
  name: 'EnterPoint',
  computed: {
    ...mapGetters([
      'history'
    ]),
    historyList () {
      return this.history.slice(0, 10)
    }
  },
  props: {
    selectedList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      linkObj: {}
    }
  },
  created () {
    // 设置id对应的name和link的object
    const result = {}
    const menuData = _.cloneDeep(menuListData)
    const subMenuPage = Object.entries(this.Enums.SYS_PERMISSION)
      .filter(entry => entry[1].type === 'PAGE')
    menuData.forEach(item => {
      if (item?.children?.length > 0) {
        item.children.forEach(cld => {
          subMenuPage.find(item => {
            if (cld.desc === item[1].name) {
              result[item[1].value] = { link: cld.link, name: cld.name }
              return true
            }
          })
        })
      }
    })
    this.linkObj = result
  },
  methods: {
    // 跳转到相应的页面
    goLink (link) {
      this.$router.push({ path: link })
    },
    // 如果未进行企业认证提示
    addEnter () {
      if (this.userInfo.tenant_id === 0) {
        this.$message.warning('您暂未进行企业认证')
      } else {
        this.$emit('add')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.link-wrapper{
  width: 176px;
  height: 40px;
  background: #F3F6FB;
  border-radius: 4px;
  line-height: 40px;
  margin-right: 20px;
  margin-bottom: 20px;
  padding:0 14px;
  cursor: pointer;

  &:hover{
    background: #5F77FD;
    color: #FFFFFF;
  }
}

.history-link{
  width: 109px;
  white-space: nowrap;
}

.line {
  width: 100%;
  height: 1px;
  border: 1px dashed #D3D3D3;
}

.title {
  height: 16px;
  font-size: 16px;
  font-weight: 500;
  color: #333333;
  margin: 20px 0;
}

.link-none{
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  padding: 40px 0 60px;
  color: #666666;

  .add-btn{
    cursor: pointer;
    padding: 10px 20px;
    color: #999999;
    border: 1px dashed #d3d3d3;
    margin-bottom: 10px;

    &:hover{
      border-color: #5f77fd;
      color: #5f77fd;
    }
  }
}
</style>
